<template>
  <div class="comment-post">
    <van-field
      autosize
      maxlength="50"
      placeholder="请输入留言"
      rows="2"
      show-word-limit
      type="textarea"
      v-model.trim="message"
    />
    <van-button :disabled="!message.length" @click="onpublishCom" class="post-btn">发布</van-button>
  </div>
</template>

<script>
import { addComments } from '@/api/comment'
export default {
  name: 'CommentPost',

  props: {
    articleId: {
      type: [Number, String, Object],
      requried: true,

    }
  },
  data () {
    return {
      message: ''
    }
  },
  methods: {
    async onpublishCom () {
      this.$toast.loading({
        message: '发布中...',
        forbidClick: true,  // 禁用背景点击
        duration: 0   // 持续事件  默认2000  0表示持续显示不关闭
      })
      try {
        const { data } = await addComments({
          target: this.articleId.toString(),
          content: this.message,
          art_id: null
        })
        // console.log(data);

        this.message = ''
        this.$emit('post-success', data.data)
        this.$toast.success('发布成功')
      } catch (err) {
        this.$toast.fail('发布失败，请重新发布')
      }
    }
  }
}
</script>
<style lang="less" scoped>
.comment-post {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .van-field {
    background: rgb(236, 235, 235);
    margin: 20px;
    font-size: 32px;
    padding: 40px 20px;
  }
  .post-btn {
    white-space: nowrap;
    border: 0;
    color: #3296fa;
    font-size: 32px;
  }
}
</style>